package com.example.administrator.day0422;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class RotateSurfView extends RelativeLayout {
    private RotateListener rotateListener;
    private RotateView rotateView;
    private Context mContext;
    private ImageView mStart;

    public void setRotateListener(RotateListener rotateListener) {
        rotateView.setRotateListener(rotateListener);
        this.rotateListener = rotateListener;
    }

    public RotateSurfView(Context context) {
        super(context);
        init(context, null);
    }

    public RotateSurfView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RotateSurfView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        mContext = context;
        //添加圆盘视图
        rotateView = new RotateView(mContext, attrs);
        RelativeLayout.LayoutParams layoutParams =
                new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
        rotateView.setLayoutParams(layoutParams);
        addView(rotateView);
        //添加开始按钮
        mStart = new ImageView(mContext);
        mStart.setImageResource(R.mipmap.node);
        RelativeLayout.LayoutParams rlStart =
                new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rlStart.addRule(RelativeLayout.CENTER_IN_PARENT);
        mStart.setLayoutParams(rlStart);
        addView(mStart);

        mStart.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rotateListener != null) {
                    rotateListener.retateBefore(mStart);
                }
            }
        });
    }

    public void startRotate(int position) {
        if (rotateView != null) {
            rotateView.startRotate(position);
        }
    }

    //记录当前是否是第一次回调onMeasure
    private boolean isFirst = true;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //视图是正方形的，所以有宽就足够了
        setMeasuredDimension(getDefaultSize(0, widthMeasureSpec), getDefaultSize(0, heightMeasureSpec));
        final int childWidthSize = getMeasuredWidth();
        //高度和宽度一样
        heightMeasureSpec = widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidthSize, MeasureSpec.EXACTLY);

        //onMeasure调用获取到当前视图的大小后
        //手动按照一定的比例计算出中间开始按钮的大小
        //再设置给开始按钮，只设置一次
        if (isFirst) {
            isFirst = false;
            //获取中间按钮的大小
            ViewTreeObserver vto = mStart.getViewTreeObserver();
            vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    mStart.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    int h = mStart.getMeasuredHeight();
                    int w = mStart.getMeasuredWidth();
                    //计算新的大小 默认为整个大小最大值的0.17
                    int newW = (int) (((float) childWidthSize) * 0.17);
                    int newH = (int) (((float) childWidthSize) * 0.17 * h / w);
                    ViewGroup.LayoutParams layoutParams = mStart.getLayoutParams();
                    layoutParams.height = newH;
                    layoutParams.width = newW;
                    mStart.setLayoutParams(layoutParams);
                }
            });
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
